package com.example.demo.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.CommonResult;
import com.example.demo.common.StatusCode;
import com.example.demo.domain.Order;
import com.example.demo.domain.Room;
import com.example.demo.dto.AdminReturnRoomDTO;
import com.example.demo.service.OrderService;
import com.example.demo.service.RoomService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuchao
 * @version 1.0
 * @date 2024/11/5
 */
@RestController("adminRoomController")
@RequestMapping("/admin")
public class RoomController {

    @Resource
    private RoomService roomService;
    @Resource
    private OrderService orderService;

    /**
     * 给房间数据添加分页
     * @param roomNumber 房间号
     * @param pageNum 第几页
     * @param pageSize 每页条数
     * @return
     */
    @GetMapping("/listRooms")
    public CommonResult<Page<Room>> listRooms(@RequestParam(required = false) String roomNumber,
                                              @RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                                              @RequestParam(required = false, defaultValue = "10", value = "pageSize") Integer pageSize
                                              ) {
        CommonResult<Page<Room>> commonResult = new CommonResult<>();

        // 在这里，要进行分页，因为拿到的数据太多，不进行分页的话，看起来就很不方便，而且看不到总的条数，
        // 主要这个集成了mybatis-plus插件，所以进行分页比较简单


        List<Room> roomList = new ArrayList<>();
        Page<Room> page = new Page<>();

        Page<Room> customerPage = new Page<>(pageNum, pageSize);
        if (roomNumber!=null && !roomNumber.isEmpty()){
            QueryWrapper<Room> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("number",roomNumber);
//            roomList = roomService.list(queryWrapper);
            page = roomService.page(customerPage,queryWrapper);
        }
        else {
            page = roomService.page(customerPage);
//            roomList = roomService.list();
        }


        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        commonResult.setData(page);

        return commonResult;
    }

    @PostMapping(value = "/roomDetail")
    public CommonResult<AdminReturnRoomDTO> roomDetail(@RequestParam("roomId") Integer roomId) {
        CommonResult<AdminReturnRoomDTO> commonResult = new CommonResult<>();

        AdminReturnRoomDTO returnRoomDTO = roomService.adminRoomDetail(roomId);

        commonResult.setData(returnRoomDTO);
        commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
        commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
        return commonResult;
    }

    @PostMapping("/addRoom")
    public CommonResult<String> addRoom(@RequestBody Room room) {
       CommonResult<String> commonResult = new CommonResult<>();
        //根据房间号查询房间
        Room roomByNumber = roomService.getOne(new QueryWrapper<Room>().eq("number", room.getNumber()));
        //如果查询到房间，则返回失败信息
        if(roomByNumber != null) {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("房间号已存在");
            return commonResult;
        }
        //保存房间信息
        boolean result = roomService.save(room);

        //根据保存结果返回不同的信息
        if (result) {
            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("添加房间成功");
        } else {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("添加房间失败");
        }

        return commonResult;
    }

    @PostMapping("/updateRoom")
    public CommonResult<String> updateRoom(@RequestBody Room room) {
        CommonResult<String> commonResult = new CommonResult<>();
        Room roomByNumber = roomService.getOne(new QueryWrapper<Room>().eq("number", room.getNumber()).ne("id", room.getId()));
        if(roomByNumber != null) {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("房间号已存在");
            return commonResult;
        }

        boolean result = roomService.updateById(room);

        if (result) {
            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("修改房间信息成功");
        } else {
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("修改房间信息失败");
        }

        return commonResult;
    }

    @PostMapping("/deleteRoom")
    public CommonResult<String> deleteRoom(@RequestParam("roomId") Integer roomId) {
        CommonResult<String> commonResult = new CommonResult<>();


        // 移除关联订单
        orderService.remove(
                new QueryWrapper<Order>().eq("room_id", roomId)
        );

        //调用roomService的removeById方法，根据房间id删除房间
        boolean result = roomService.removeById(roomId);

        //根据删除结果，返回不同的结果
        if (result) {
            //根据房间id查询订单
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("room_id", roomId);
            orderService.remove(queryWrapper);

            //设置返回结果
            commonResult.setCode(StatusCode.COMMON_SUCCESS.getCode());
            commonResult.setMessage(StatusCode.COMMON_SUCCESS.getMessage());
            commonResult.setData("删除房间成功");
        } else {
            //设置返回结果
            commonResult.setCode(StatusCode.COMMON_FAIL.getCode());
            commonResult.setMessage(StatusCode.COMMON_FAIL.getMessage());
            commonResult.setData("删除房间失败");
        }
        return commonResult;
    }

}
